﻿namespace HRM.Update.Process
{
    using ComponentAce.Compression.Archiver;
    using ComponentAce.Compression.ZipForge;
    using System;
    using System.IO;
    using System.Net;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Windows.Forms;

    public class ProcessDownLoad
    {
        private bool bool_0;
        private const int int_0 = 0x4000;
        private Stream stream_0;
        private Stream stream_1;
        private Thread thread_0;
        private UpdateFile updateFile_0;
        private WebRequest webRequest_0;
        private WebResponse webResponse_0;

        public event DownLoadStatusEventHander DownLoadStatus;

        public event ExtractFileEventHander ExtractFile;

        public event SendTotalSizeEventHander SendTotalSize;

        public event UpdateProcCallbackEventHander UpdateProcCallback;

        public void Close()
        {
            if (this.stream_1 != null)
            {
                this.stream_1.Close();
            }
            if (this.stream_0 != null)
            {
                this.stream_0.Close();
            }
            if ((this.thread_0 != null) && this.thread_0.IsAlive)
            {
                this.thread_0.Abort();
            }
        }

        private void method_0(string string_0)
        {
            ExtractFileEventHander hander = this.ExtractFile;
            if (hander != null)
            {
                hander(string_0);
            }
        }

        private void method_1(DownloaderState downloaderState_0)
        {
            DownLoadStatusEventHander hander = this.DownLoadStatus;
            if (hander != null)
            {
                hander(downloaderState_0);
            }
        }

        private void method_2(long long_0)
        {
            SendTotalSizeEventHander hander = this.SendTotalSize;
            if (hander != null)
            {
                hander(long_0);
            }
        }

        private void method_3(long long_0, long long_1)
        {
            UpdateProcCallbackEventHander hander = this.UpdateProcCallback;
            if (hander != null)
            {
                hander(long_0, long_1);
            }
        }

        private void method_4(object object_0)
        {
            this.updateFile_0 = (UpdateFile) object_0;
            string requestUriString = this.updateFile_0.UpdateLink + "/" + this.updateFile_0.ZipName + ".zip";
            string path = Application.StartupPath + @"\Update\" + this.updateFile_0.ZipName + ".zip";
            this.stream_1 = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
            try
            {
                int num2;
                Thread.Sleep(0x7d0);
                this.method_1(DownloaderState.Preparing);
                this.webRequest_0 = WebRequest.Create(requestUriString);
                try
                {
                    this.webResponse_0 = this.webRequest_0.GetResponse();
                }
                catch (Exception)
                {
                    this.method_1(DownloaderState.EndedWithError);
                    this.bool_0 = true;
                    return;
                }
                this.stream_0 = this.webResponse_0.GetResponseStream();
                long contentLength = this.webResponse_0.ContentLength;
                Thread.Sleep(0x3e8);
                this.method_2(contentLength);
                byte[] buffer = new byte[0x4000];
                Thread.Sleep(0x3e8);
                this.method_1(DownloaderState.Prepared);
                while ((num2 = this.stream_0.Read(buffer, 0, buffer.Length)) > 0)
                {
                    this.stream_1.Write(buffer, 0, num2);
                    this.method_3(this.stream_1.Length, contentLength);
                    this.method_1(DownloaderState.Working);
                }
                if (num2 == 0)
                {
                    this.method_1(DownloaderState.Ended);
                }
            }
            catch (Exception)
            {
                if (this.stream_1 != null)
                {
                    this.stream_1.Close();
                }
                if (this.stream_0 != null)
                {
                    this.stream_0.Close();
                }
                if (this.thread_0.IsAlive)
                {
                    this.thread_0.Abort();
                }
            }
            finally
            {
                if (this.stream_1 != null)
                {
                    this.stream_1.Close();
                }
                if (this.stream_0 != null)
                {
                    this.stream_0.Close();
                }
                if (!this.bool_0)
                {
                    this.method_1(DownloaderState.PrepareExtract);
                    this.method_5(path);
                }
            }
        }

        private void method_5(string string_0)
        {
            ComponentAce.Compression.ZipForge.ZipForge forge = new ComponentAce.Compression.ZipForge.ZipForge();
            try
            {
                forge.FileName = string_0;
                forge.OpenArchive(FileMode.Open);
                forge.OnFileProgress += new BaseArchiver.OnFileProgressDelegate(this.method_6);
                string str = Class0.smethod_1();
                if (string.IsNullOrEmpty(str))
                {
                    forge.BaseDir = Application.StartupPath;
                }
                else if (str == ".")
                {
                    forge.BaseDir = Application.StartupPath;
                }
                else
                {
                    string str2 = Application.StartupPath + @"\" + Class0.smethod_1();
                    if (createDirecoty(str2))
                    {
                        forge.BaseDir = str2;
                    }
                }
                forge.ExtractFiles("*.*");
                forge.CloseArchive();
                Class0.funcupdateFile2(this.updateFile_0);
                Class0.funcupdateFile1(this.updateFile_0);
                if (System.IO.File.Exists(string_0))
                {
                    System.IO.File.Delete(string_0);
                    this.method_1(DownloaderState.EndExtract);
                }
            }
            catch (ArchiverException exception)
            {
                MessageBox.Show(string.Concat(new object[] { "Message: ", exception.Message, "\t Error code: ", exception.ErrorCode }));
                Application.ExitThread();
                Application.Exit();
            }
        }

        private void method_6(object object_0, string string_0, double double_0, TimeSpan timeSpan_0, TimeSpan timeSpan_1, ProcessOperation processOperation_0, ProgressPhase progressPhase_0, ref bool bool_1)
        {
            this.method_0("... Cập nhật <" + string_0 + ">, " + double_0.ToString("f2") + "%");
        }

        private static unsafe bool createDirecoty(string string_0)
        {
            bool exist = false;
            try
            {
                exist = Directory.Exists(string_0);
                if (!exist)
                {
                    Directory.CreateDirectory(string_0);
                }
                exist = true;
            }
            catch
            {
            }
            return exist;
        }

        public void Start(UpdateFile obj)
        {
            this.thread_0 = new Thread(new ParameterizedThreadStart(this.method_4));
            this.thread_0.Start(obj);
        }

        public delegate void DownLoadStatusEventHander(DownloaderState state);

        public delegate void ExtractFileEventHander(string times);

        public delegate void SendTotalSizeEventHander(long size);

        public delegate void UpdateProcCallbackEventHander(long bytesRead, long totalBytes);
    }
}

